package com.xumt.hwHMSdemo.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xumt.hwHMSdemo.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ChanceAddressAdapter extends RecyclerView.Adapter<ChanceAddressAdapter.ViewHolder> {
    private Context context;
    private List<Map<String,Object>> addresslist=new ArrayList<>();
    public static String name;
    public static String phone;
    public static String Address;
    public ChanceAddressAdapter(Context context,List<Map<String,Object>> addresslist)
    {
        this.context=context;
       this.addresslist=addresslist;
    }
    static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView name;
        private TextView phone;
        private TextView address;
        private CheckBox checkBox;
        private RecyclerView recyclerView;
        public ViewHolder(View view)
        {
            super(view);
            recyclerView=(RecyclerView)view.findViewById(R.id.addressRecyclerView);
            name=(TextView)view.findViewById(R.id.name_Textview);
            phone=(TextView)view.findViewById(R.id.phone_Textview);
            address=(TextView)view.findViewById(R.id.Address_Textview);
            checkBox=(CheckBox)view.findViewById(R.id.chanceaddressbox);
//            RecyclerView.LayoutManager manager=new LinearLayoutManager(view.getContext());
//            manager.setAutoMeasureEnabled(true);
//            recyclerView.setLayoutManager(manager);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item2,parent,false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.name.setText(String.valueOf(addresslist.get(position).get("name")));
        holder.address.setText(String.valueOf(addresslist.get(position).get("address")));
        holder.phone.setText(String.valueOf(addresslist.get(position).get("phone")));
        if (Integer.parseInt(addresslist.get(position).get("moren").toString())==1)
        {
            holder.checkBox.setChecked(true);
        }
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.checkBox.isChecked())
                {
                    name=String.valueOf(holder.name.getText());
                    phone=String.valueOf(holder.phone.getText());
                    Address=String.valueOf(holder.address.getText());
                }

            }
        });


    }

    @Override
    public int getItemCount() {
        return addresslist.size();
    }




}
